<?php

/**
 * Created by PhpStorm.
 * Author: 赵凯
 * Date: 2020/1/6
 * Time: 18:06
 */
namespace api\message\controller;

use api\common\service\CurlService;
use think\Controller;

class PushMessageController extends Controller
{
    /**
     * 发送模板消息
     * $openid  用户openid
     * $message  发送的消息 array
     */
    public function send($openid,$template_id,$message)
    {
        $data = [
            'openid' => $openid,
            'template_id' => $template_id,
            'data' =>$message
        ];

        $ret = $this->templateMessage($data);
        if($ret)
        {
            return $ret;
        }else{
            return 'faile';
        }

    }

    /**
     * @param $params [参数]
     * @param $appid [小程序appid]
     * @param $secret [小程序secret]
     * @param $type [模板消息的类型] 如：审核，通知， 签到 。。。。
                     {
                  "touser": "OPENID",
                  "template_id": "TEMPLATE_ID",
                  "page": "index",
                  "miniprogram_state":"developer",
                  "lang":"zh_CN",
                  "data": {
                      "number01": {
                          "value": "339208499"
                      },
                      "date01": {
                          "value": "2015年01月05日"
                      },
                      "site01": {
                          "value": "TIT创意园"
                      } ,
                      "site02": {
                          "value": "广州市新港中路397号"
                      }
                  }
                }
     */
    public function templateMessage($params)
    {
        if(config('is_ceshi'))
            {
                $appid='wxd4046db59ec2e394'; 
                $secret='18cc56a45baa9234c37c108cc2f2bfe3';
                $template = [
                        1 => 'CXl0IHuTvir3zWsI46hgtJrCdfDxP8KmNhvuwN8JYck',//安装项目进场通知
                        2 => '4yXWoDo_wa2d8RfnOx8bNJGeHwa0Al6Is68buqecF_0',//安装单派工通知
                        3 => 'IUBtWK_9XrHEdKCXX7fHxoVnB018zDbApIVJb2KN8_w',//安装单退回通知
                        4 => 'eroGvNFuHXkfHN9-gcOIzRKuzsV4I6QRuEeSozfRPPk',//安装单延迟交付审核提醒
                        5 => 'PSnXlWGvfGJPcLKnhildvw2oCNqE__io6XPgKRXQfgk',//安装单自动交付通知
                        6 =>'',
                    ];
            }else{
                $appid='wx65c369695c51c26e';
                $secret='064ce847258d9d769e6cfb87a1ba06e2';
                $template = [
                        1 =>'',
                        2 =>'',
                        3 =>'',
                        4 =>'',
                        5 =>'',
                        6 =>'',
                    ];
            }
        # 模板关键字
        
        $postData = [
            'touser'        =>  $params['openid'],
            'template_id'   =>  $template[$params['template_id']],//这是模板id,需要去申请
            // 'page'          =>  $params['page'],
//            'form_id'       =>  $params['form_id'],
            'data'          =>  $params['data'],
        ];

        # user_token 必须全局获取|存储
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
        $result =CurlService::curlRequest($url,[]);
        $result = json_decode($result);
        $access_token = $result->access_token;
//        $urls = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$access_token;
        $urls = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$access_token;
        $postData = json_encode($postData);
        $ret =CurlService::curlRequest($urls, $postData,'POST');
        $ret = json_decode($ret);
        return $ret->errcode;
    }
}